package com.efast.dmom.equipment.opc.client;

import java.io.IOException;
import java.util.Properties;

import org.openscada.opc.lib.common.ConnectionInformation;

public class OPCConfiguration {

	private final static ConnectionInformation ci;
	private final static Properties prop;
	private final static String CONFIG_USERNAME = "CONFIG_USERNAME";
	private final static String CONFIG_PASSWORD = "CONFIG_PASSWORD";
	private final static String CONFIG_HOST = "CONFIG_HOST";
	private final static String CONFIG_DOMAIN = "CONFIG_DOMAIN";
	private final static String CONFIG_CLSID = "CONFIG_CLSID";
	private final static String CONFIG_PROGID = "CONFIG_PROGID";
	private final static String CONFIG_FILE_NAME = "connection.properties";

	static {
		ci = new ConnectionInformation();
		prop = new Properties();
		try {
			prop.load(OPCConfiguration.class.getClassLoader().getResourceAsStream(CONFIG_FILE_NAME));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getEntryValue(String name) {
		return prop.getProperty(name);
	}

	public static ConnectionInformation getCLSIDConnInfo() {
		ci.setProgId(null);
		getConnInfo();
		ci.setClsid(prop.getProperty(CONFIG_CLSID));
		return ci;
	}

	public static ConnectionInformation getPROGIDConnInfo() {
		ci.setClsid(null);
		getConnInfo();
		ci.setProgId(prop.getProperty(CONFIG_PROGID));
		return ci;
	}

	private static void getConnInfo() {
		ci.setHost(prop.getProperty(CONFIG_HOST));
		ci.setDomain(prop.getProperty(CONFIG_DOMAIN));
		ci.setUser(prop.getProperty(CONFIG_USERNAME));
		ci.setPassword(prop.getProperty(CONFIG_PASSWORD));
	}

}
